package com.hhd.pdf.component.functions;

import com.hhd.pdf.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class PdfFunctionExecutor {

    @Autowired
    @Lazy
    List<PdfFunction> pdfFunctions;

    public <T> T exeute(PdfFunctionContext context, Class<T> booleanClass) {
        if (CommonUtils.isEmpty(context.getFunction())) {
            throw new RuntimeException("function不能为空!: " + context.getElement().asXML());
        }
        PdfFunction function = getFunction(context);
        if (CommonUtils.isEmpty(function)) {
            throw new RuntimeException("没有找到函数: " + context.getFunction());
        }
        return (T) function.execute(context);
    }

    private PdfFunction getFunction(PdfFunctionContext context) {
        for (PdfFunction function : pdfFunctions) {
            if (context.getFunction().equals(function.getFunctionName())) {
                return function;
            }
        }
        return null;
    }
}
